
package com.starshineworks.util;

import com.starshineworks.entities.EntityE;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.*;

public class ObjectBuilder {

    public String buildMenu(String               type,
                            Integer              rowid, 
                            Integer              menuid, 
                            Map<String,String[]> mp) {
        String y = "";
        Menu m = MenuManager.getMenuManager(type, rowid, mp).createRequestMenu(menuid);
        if (m != null) {
            y = m.toHTML();
        }
        return y;
    }

    public String buildList(String  type, 
                            int     view,
                            boolean add) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        String none = "FUNCTION NOT IMPLEMENTED";
        List<Object> desc = new ArrayList<Object>();
        desc.add(none);
        String pkg = EntityE.packageName();
        try {
            //Filter filter = new Filter(filt);
            Class cls = Class.forName(pkg + "." + type);
            List<EntityE> list = EntityE.retrieve(cls.newInstance());
            if (!list.isEmpty()) {
                desc.clear();
                for (EntityE ent : list) {
                    ent.refreshDisplay(view);
                    desc.add(ent);
                }
            }
            if (add) {
                EntityE addNew = (EntityE)cls.newInstance();
                addNew._name = "--new-entry--";
                addNew.refreshDisplay(view);
                desc.add(0, addNew);
            }
        } catch (Exception e) {
            Util.handleException(e);
        }
        String str = "";
        try {
            str = mapper.writeValueAsString(desc);
        } catch (Exception e) {
            Util.handleException(e);
        }
        return str;
    }
    
}
